// pages/citylist/citylist.js
//获取应用实例
const app = getApp()
const util = require("../../utils/util.js");
var QQMapWX = require('../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
// NIQBZ-HL2CS-J3QOV-6BIV5-TFFNE-PFFZY
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hidden: true,
        citySelected: '深圳',
        city: '深圳',
        showCity: true,
        usualData: [],
        isUsualcity: false,
        // dwcity: wx.getStorageSync('dwcity'),
        // dwcitycode: wx.getStorageSync('dwcitycode')
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        this.setData({
            pagetype: options.pagetype ? options.pagetype : '',
        })
        util.getUrl(app.globalData.url).then((res) => {
            util.cityList().then((res) => {
                let allcity = res.allcity;
                let _py = Object.keys(res.cityData)
                let pyarr = ['hot'];
                for (let i of _py) {
                    pyarr.push(i)
                }
                this.setData({
                    cityData: res.cityData,
                    _py: pyarr,
                });
                qqmapsdk.reverseGeocoder({
                    success: (res) => {
                        let dwcity = res.result.address_component.city;
                        let citydata;
                        if (dwcity)
                            citydata = allcity.filter(v => {
                                if (dwcity.indexOf(v.cityname) != -1) {
                                    return v;
                                }
                            });
                        if (citydata && Array.isArray(citydata) && citydata[0]) {
                            let dwcitycode = citydata[0].citycode;
                            ks.setStorageSync("dwcitycode", dwcitycode)
                            this.setData({
                                dwcity: citydata[0].cityname,
                                dwcitycode,
                            })
                        }
                    },
                });
            })

            this.getHotCity();
        })
    },

    // 获取热门城市
    getHotCity() {
        util.request('city.php', {
            action: 'hot'
        }, 'GET').then(res => {
            this.setData({
                hotCity: res.data.data,
            })
        })
    },

    // 输入事件
    inputTap(e) {
        let keywords = e.detail.value;
        this.setData({
            keywords,
            showCity: false
        })
        util.cityList(keywords).then((res) => {
            this.setData({
                cityRes: res.cityRes,
            })
        })
    },

    // 输入框回车搜索事件
    confirmTap(e) {
        let keywords = e.detail.value;
        this.setData({
            keywords,
            showCity: false
        })
        util.cityList(keywords).then((res) => {
            this.setData({
                cityRes: res.cityRes,
            })
        })
    },

    // 点击搜索
    searchCity() {
        this.setData({
            // keywords,
            showCity: false
        })
        util.cityList(this.data.keywords).then((res) => {
            this.setData({
                cityRes: res.cityRes,
            })
        })
    },

    // 输入框删除icon事件
    delTap() {
        this.setData({
            keywords: "",
            showCity: true,
        })
    },

    //选择城市
    selectCity: function(e) {
        var dataset = e.currentTarget.dataset;
        let usualData = this.data.usualData;
        this.setData({
            citySelected: dataset.cityname,
        });
        ks.setStorageSync('city', dataset.cityname)
        ks.setStorageSync('citycode', dataset.citycode)

        if (this.data.pagetype == 'index') {
            ks.reLaunch({
                url: '../index/index?city=' + dataset.citycode,
            })
        } else {
            ks.reLaunch({
                url: '../bsy/bsy?city=' + dataset.citycode,
            })
        }

        if (usualData.length >= 1) {
            if (usualData.findIndex((n) => n.citycode == dataset.citycode) < 0) {
                usualData.unshift({
                    cityname: dataset.cityname,
                    citycode: dataset.citycode,
                })
            }
        } else {
            usualData.unshift({
                cityname: dataset.cityname,
                citycode: dataset.citycode,
            })
        }

        //将常用城市数组整体储存到缓存中
        ks.setStorageSync('usualData', usualData)
        this.setData({
            usualData: ks.getStorageSync('usualData')
        })
    },
    touchstart: function(e) {
        this.setData({
            index: e.currentTarget.dataset.index,
            Mstart: e.changedTouches[0].pageX
        });
    },
    touchmove: function(e) {
        var history = this.data.historyList;
        var move = this.data.Mstart - e.changedTouches[0].pageX;
        history[this.data.index].x = move > 0 ? -move : 0;
        this.setData({
            historyList: history
        });
    },
    touchend: function(e) {
        var history = this.data.historyList;
        var move = this.data.Mstart - e.changedTouches[0].pageX;
        history[this.data.index].x = move > 100 ? -180 : 0;
        this.setData({
            historyList: history
        });
    },

    //获取文字信息
    getPy: function(e) {
        this.setData({
            hidden: false,
            showPy: e.target.id,
        })
    },

    setPy: function(e) {
        this.setData({
            hidden: true,
            scrollTopId: this.data.showPy
        })
    },

    //滑动选择城市
    tMove: function(e) {
        var y = e.touches[0].clientY,
            offsettop = e.currentTarget.offsetTop;

        //判断选择区域,只有在选择区才会生效
        if (y > offsettop) {
            var num = parseInt((y - offsettop) / 12);
            this.setData({
                showPy: this.data._py[num]
            })
        };
    },

    //触发全部开始选择
    tStart: function() {
        this.setData({
            hidden: false
        })
    },

    //触发结束选择
    tEnd: function() {
        this.setData({
            hidden: true,
            scrollTopId: this.data.showPy
        })
    },

    // 清除常用城市事件
    onDelusualCity: function(e) {
        ks.removeStorageSync('usualData')
        this.setData({
            usualData: [],
            isUsualcity: false
        })
    },

    onShow() {
        if (ks.getStorageSync('usualData')) {
            this.setData({
                usualData: ks.getStorageSync('usualData'),
                isUsualcity: true,
            })
        }
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})